package com.example.neusoft.fragmenttest.adatper;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.neusoft.fragmenttest.R;
import com.example.neusoft.fragmenttest.bean.ArticleBean;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/5.
 */

public class ArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //1
    private List<ArticleBean> list;//保存要显示的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局

    public ArticleAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }
    //2
    public void setList(List<ArticleBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    //3
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle, tvdescript, tvtime;
        Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tvtitle = (TextView) itemView.findViewById(R.id.textView);
            tvdescript = (TextView) itemView.findViewById(R.id.textView2);
            tvtime = (TextView) itemView.findViewById(R.id.textView3);
            button = (Button) itemView.findViewById(R.id.button);
        }
    }

    //4
    // 这个方法主要生成为每个Item inflater出一个View，但是该方法返回的是一个ViewHolder。
    // 该方法把View直接封装在ViewHolder中，然后我们面向的是ViewHolder这个实例，当然这个ViewHolder需要我们自己去编写。
    // 直接省去了当初的convertView.setTag(holder)和convertView.getTag()这些繁琐的步骤。
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflate第一个参数要加载的布局id，第二个参数是指给该布局的外部再嵌套一层父布局，如果不需要就直接传null
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    //5给ViewHolder中的控件填充值,设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ArticleBean articleBean = list.get(position);
        if (articleBean == null)
            return;
        ViewHolder viewholder = (ViewHolder) holder;
        //viewholder.imageView.setImageResource(article.getImgid());
        viewholder.tvtitle.setText(articleBean.getName());
        viewholder.tvdescript.setText(articleBean.getDescription());
        viewholder.tvtime.setText(articleBean.getUpdate_time());
        //条目中按钮监听事件
        viewholder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
                //本例为测试，实际案例中删除条目应该会影响到数据库中
            }
        });
        //条目点击监听事件
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前条目id
                String strid = String.valueOf(articleBean.getId());
                Toast.makeText(context, strid, Toast.LENGTH_SHORT).show();

            }
        });

        viewholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                showdialog();
                return true;
            }
        });
    }
    //6
    //总共有多少个条目
    @Override
    public int getItemCount() {
        if (list == null) return 0;
        else return list.size();
    }

    private void showdialog() {
        final View view=layoutInflater.inflate(R.layout.dialoglayout,null);
        AlertDialog dialog=new AlertDialog.Builder(context)
                .setTitle("对话框标题")
                .setIcon(R.mipmap.ic_launcher)
                .setView(view)
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText etname=(EditText)view.findViewById(R.id.editText);
                        EditText etpass=(EditText)view.findViewById(R.id.editText2);
                        String msg=etname.getText().toString()+etpass.getText().toString();
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create();
        dialog.show();

    }
}
